//
//  RoomsManager.m
//  mUBB
//
//  Created by Cosmin Stirbu on 2/26/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "RoomsManager.h"
#import "WSManager+Rooms.h"
#import "Room.h"

@implementation RoomsManager


// Singleton
+ (RoomsManager *)instance
{
    static RoomsManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[RoomsManager alloc] init];
        // Do any other initialisation stuff here
    });
    return instance;
}


- (Ticket *)getRoomsByKeyword:(NSString *)keyWord take:(NSInteger)take skip:(NSInteger)skip delegate:(id<RoomsProtocol>)delegate {
    return [[WSManager instance] getRoomsByKeyword:keyWord take:take skip:skip delegate:delegate];
}

- (Ticket *)getDetailsForRoom:(Room *)room delegate:(id<RoomsProtocol>)delegate {
    return [[WSManager instance] getDetailsForRoom:room delegate:delegate];
}

- (Ticket *)getRoomsForFaculty:(Faculty *)faculty delegate:(id<RoomsProtocol>)delegate {
    return [[WSManager instance] getRoomsForFaculty:faculty delegate:delegate];
}

- (NSDictionary *)groupRoomsByBuildings:(NSArray *)rooms {
    NSMutableDictionary *groupedRooms = [NSMutableDictionary new];
    for (Room *room in rooms) {
        NSArray *allKeys = [groupedRooms allKeys];
        if (![allKeys containsObject:room.building.name]) {
            NSMutableArray *roomsForBuilding = [NSMutableArray new];
            [roomsForBuilding addObject:room];
            [groupedRooms setObject:roomsForBuilding forKey:room.building.name];
        } else {
            NSMutableArray *roomsForBuilding = [groupedRooms objectForKey:room.building.name];
            [roomsForBuilding addObject:room];
        }
    }
    return groupedRooms;
}

@end
